<?php 

$listing_id = $_GET['listing_id'];
$group_id = $_GET['group_id'];
//$listing_id = '242801';
//$username='matt';

$actives = ($_GET['actives']!='false')? "'Active', ": "";
$pendings = ($_GET['pendings']!='false')? "'Pending', ": "";
$solds = ($_GET['solds']!='false')? "'Sold'": "";
$city_name = $_GET['fl_city'];
$cs = ($_GET['cs']=='true')? "'COMMING_SOON'": "";
if($_GET['cs']=='true'){
$in = $cs;
}else{
$in = $actives.$pendings.$solds;
}
//$in = $actives.$pendings.$solds;
$in = trim($in);
$in = rtrim($in, ',');
$in = "(".$in.")";

include("../db/opendb.php");

include("functions.php");


if($city_name != '')
{
$query = "select i.Path as IMAGE,l.ZIPCODE, l.TITLE, l.LONGITUDE, l.LATITUDE, l.IVR, t.Name, l.LANDING_PAGE, l.MLSNUM, l.VIRTUAL_TOUR_URL, l.display_zillow, l.STYLE, l.YEARBUILT, l.ASSOCFEE, l.STORIES, l.OID, l.LISTSTATUS, l.STREETNUM, l.STREETDIR, l.STREETNAME, l.STREETSUFFIX, l.UNITFLOORNUM, l.CITY, l.STATE, l.REMARKS, l.BEDS, l.BATHSTOTAL, l.SQFTTOTAL, l.PARKINGCOVERED, l.LISTPRICE, l.LISTING_AGENT from PROPERTIES l inner join Prop_Owner_Con p on p.Prop_OID = l.OID inner join Users u on u.ID = p.Owner left join Listing_Images i on (i.Listing_ID = l.OID and i.Main_Photo = 1) inner join Prop_Types t on t.ID = l.PROPTYPE where l.OID = '".$listing_id."' and l.CITY='".$city_name."' and l.LISTSTATUS in ".$in." order by i.sort_order";
}
else
{
$query = "select i.Path as IMAGE,l.ZIPCODE, l.TITLE, l.LONGITUDE, l.LATITUDE, l.IVR, t.Name, l.LANDING_PAGE, l.MLSNUM, l.VIRTUAL_TOUR_URL, l.display_zillow, l.STYLE, l.YEARBUILT, l.ASSOCFEE, l.STORIES, l.OID, l.LISTSTATUS, l.STREETNUM, l.STREETDIR, l.STREETNAME, l.STREETSUFFIX, l.UNITFLOORNUM, l.CITY, l.STATE, l.REMARKS, l.BEDS, l.BATHSTOTAL, l.SQFTTOTAL, l.PARKINGCOVERED, l.LISTPRICE, l.LISTING_AGENT from PROPERTIES l inner join Prop_Owner_Con p on p.Prop_OID = l.OID inner join Users u on u.ID = p.Owner left join Listing_Images i on (i.Listing_ID = l.OID and i.Main_Photo = 1) inner join Prop_Types t on t.ID = l.PROPTYPE where l.OID = '".$listing_id."' and l.LISTSTATUS in ".$in." order by i.sort_order";
}


$result = mysql_query($query) or die ("SQL Error".mysql_error());

$row = mysql_fetch_assoc($result);

if(!$row)
{
$No_OID = 0;
$xml = "<listings>\n<listing>\n";
$xml .= "\t<oid>".$No_OID."</oid>\n";
$xml .= "</listing>\n</listings>";

}
else
{

$tmpREMARKS = convertDoc2HTML($row['REMARKS']);
$newREMARKS = htmlentities2unicodeentities($tmpREMARKS);

$xml = "<listings>\n<listing>\n";
$image = ($row[IMAGE]!='')? 'http://realivent.com/listing_images/resize_'.$row['IMAGE'] :'http://realivent.com/listing_images/no_photo.gif';
$t_image = ($row[IMAGE]!='')? 'http://realivent.com/listing_images/t_'.$row['IMAGE'] :'http://realivent.com/listing_images/no_photo.gif';
$xml .= "\t<main_image>".$image."</main_image>\n";
$xml .= "\t<main_t_image>".$t_image."</main_t_image>\n";
$xml .= "\t<oid>".$row['OID']."</oid>\n";
$xml .= "\t<streetnum>".$row['STREETNUM']."</streetnum>\n";
$xml .= "\t<streetdir>".$row['STREETDIR']."</streetdir>\n";
$xml .= "\t<streetname>".$row['STREETNAME']."</streetname>\n";
$xml .= "\t<streetsuffix>".$row['STREETSUFFIX']."</streetsuffix>\n";
$xml .= "\t<title>".urlencode($row['TITLE'])."</title>\n";
$xml .= "\t<unit>".$row['UNITFLOORNUM']."</unit>\n";
$xml .= "\t<city>".$row['CITY']."</city>\n";
$xml .= "\t<state>".$row['STATE']."</state>\n";
$xml .= "\t<beds>".$row['BEDS']."</beds>\n";
$xml .= "\t<baths>".$row['BATHSTOTAL']."</baths>\n";
$xml .= "\t<sqft>".$row['SQFTTOTAL']."</sqft>\n";
$xml .= "\t<parkingcovered>".$row['PARKINGCOVERED']."</parkingcovered>\n";
$xml .= "\t<price>".$row['LISTPRICE']."</price>\n";
//$xml .= "\t<remarks><![CDATA[".$row['REMARKS']."]]></remarks>\n";
$xml .= "\t<remarks><![CDATA[".$newREMARKS."]]></remarks>\n";
$xml .= "\t<status>".$row['LISTSTATUS']."</status>\n";
$xml .= "\t<ivr>".$row['IVR']."</ivr>\n";
$xml .= "\t<type>".$row['Name']."</type>\n";
$xml .= "\t<stories>".$row['STORIES']."</stories>\n";
$xml .= "\t<assocfee>".$row['ASSOCFEE']."</assocfee>\n";
$xml .= "\t<yearbuilt>".$row['YEARBUILT']."</yearbuilt>\n";
$xml .= "\t<style>".$row['STYLE']."</style>\n";
$xml .= "\t<mlsnum>".$row['MLSNUM']."</mlsnum>\n";
$xml .= "\t<virtual_tour_url>".urlencode($row['VIRTUAL_TOUR_URL'])."</virtual_tour_url>\n";
$xml .= "\t<landing_page>".$row['LANDING_PAGE']."</landing_page>\n";
$xml .= "\t<longitude>".$row['LONGITUDE']."</longitude>\n";
$xml .= "\t<latitude>".$row['LATITUDE']."</latitude>\n";
$xml .= "\t<zipcode>".$row['ZIPCODE']."</zipcode>\n";
$xml .= "\t<displayzillow>".$row['display_zillow']."</displayzillow>\n";
$xml .= "\t<listingagent>".$row['LISTING_AGENT']."</listingagent>\n";

$xml .= "\t<images>\n";

$query_images = "select * from Listing_Images where Listing_ID = '".$row['OID']."' and Type = 'IMAGE' order by sort_order";
$result_images = mysql_query($query_images) or die ("SQL Error".mysql_error());
if(mysql_num_rows($result_images))
{
	while($rowi = mysql_fetch_assoc($result_images))
	{
		$xml .= "\t\t<image>http://realivent.com/listing_images/t_".$rowi['Path']."</image>\n";
	}
}
else
{
$xml .= "\t\t<image>http://realivent.com/listing_images/nia_thumb.gif</image>\n";
}

$xml .= "\t</images>\n";

$xml .= "\t<agent>\n";

$query_agent = "select u.* from Users u inner join Prop_Owner_Con c on (c.Owner = u.ID and c.Prop_OID = '".$row['OID']."')";
$result_agent = mysql_query($query_agent) or die ("SQL Error".mysql_error());
$rowa = mysql_fetch_assoc($result_agent);


	$xml .= "\t\t<first_name>".urlencode($rowa['First_Name'])."</first_name>\n";
	$xml .= "\t\t<last_name>".urlencode($rowa['Last_Name'])."</last_name>\n";
	$xml .= "\t\t<phone>".$rowa['Phone']."</phone>\n";
	$xml .= "\t\t<email>".$rowa['Email']."</email>\n";
	$xml .= "\t\t<brokerage><![CDATA[".$rowa['Brokerage']."]]></brokerage>\n";
	$xml .= "\t\t<website>".urlencode($rowa['Website'])."</website>\n";
	$xml .= "\t\t<image>http://realivent.com/user_images/t75_".$rowa['Image']."</image>\n";
	$xml .= "\t\t<logo>http://realivent.com/user_images/t_".$rowa['Company_Logo']."</logo>\n";


$xml .= "\t</agent>\n";
$xml .= "\t<events>\n";

$query_event = "select e.* from Events e where Listing_ID = '".$row['OID']."'";
$result_event = mysql_query($query_event) or die ("SQL Error".mysql_error());
while($rowe = mysql_fetch_assoc($result_event))
{
	$xml .= "\t\t<event>\n";
	$xml .= "\t\t<event_type>".$rowe['Event_Type']."</event_type>\n";
	$xml .= "\t\t<event_date>".date("F j, Y", strtotime($rowe['Event_Date']))."</event_date>\n";
	$xml .= "\t\t<from>".$rowe['Time_From']."</from>\n";
	$xml .= "\t\t<to>".$rowe['Time_To']."</to>\n";
	$xml .= "\t\t<time_fix>".$rowe['Time_Fix']."</time_fix>\n";
	$xml .= "\t\t<notes><![CDATA[".$rowe['Notes']."]]></notes>\n";
	$xml .= "\t\t<title><![CDATA[".$rowe['Title']."]]></title>\n";
	$xml .= "\t\t</event>\n";
}

$xml .= "\t</events>\n";

$xml .= "\t<features>\n";

$xml .= "\t\t<kitchen>\n";
$query_features = "select f.* from Prop_Features f where Listing_ID = '".$row['OID']."' and Type = 'Kitchen'";
$result_features = mysql_query($query_features) or die ("SQL Error".mysql_error());
while($rowf = mysql_fetch_assoc($result_features))
{
	$xml .= "\t\t\t<feature>".$rowf['Feature']."</feature>\n";
}
$xml .= "\t\t</kitchen>\n";
$xml .= "\t\t<interior>\n";
$query_features = "select f.* from Prop_Features f where Listing_ID = '".$row['OID']."' and Type = 'Interior'";
$result_features = mysql_query($query_features) or die ("SQL Error".mysql_error());
while($rowf = mysql_fetch_assoc($result_features))
{
	$xml .= "\t\t\t<feature>".$rowf['Feature']."</feature>\n";
}
$xml .= "\t\t</interior>\n";
$xml .= "\t\t<exterior>\n";
$query_features = "select f.* from Prop_Features f where Listing_ID = '".$row['OID']."' and Type = 'Exterior'";
$result_features = mysql_query($query_features) or die ("SQL Error".mysql_error());
while($rowf = mysql_fetch_assoc($result_features))
{
	$xml .= "\t\t\t<feature>".$rowf['Feature']."</feature>\n";
}
$xml .= "\t\t</exterior>\n";


$xml .= "\t</features>\n";

if(isset($_GET['group_id']))
{
include("opendb_crm.php");
$query1 = "select * from document_grps where group_id = '".$group_id."' and group_owner = '".$rowa['ID']."'";
$result1 = mysql_query($query1) or die ("SQL Error".mysql_error());
$row1 = mysql_fetch_assoc($result1);
$xml .= "\t<docs>\n";
if($row1['document_ids']!='')
{
$doc_id = explode(",", $row1['document_ids']);
			foreach($doc_id as $id)
				{
				$xml .= "\t\t<doc>\n";
				$dsql = "Select d.document_name, d.id from documents d where created_by = '".$rowa['ID']."' and id = '".$id."'";
				$dresult = mysql_query($dsql) or die ("SQL Error".mysql_error());
				$drow=mysql_fetch_assoc($dresult);
				
				$xml .= "\t\t\t<id>".$drow['id']."</id>\n";
				$xml .= "\t\t\t<name><![CDATA[".$drow['document_name']."]]></name>\n";
				$xml .= "\t\t</doc>\n";
				
				}

}
$xml .= "\t</docs>\n";
}

$xml .= "</listing>\n</listings>";

}
echo $xml;
?>
